home *** CD-ROM | disk | FTP | other *** search
/ Aminet 22 / Aminet 22 (1997)(GTI - Schatztruhe)[!][Dec 1997].iso / Aminet / dev / src / ConfigFileSrc.lha / ConfigFileSrc12 / Library / Funcs / Open&Close.c < prev    next >
Encoding:
Text File  |  1997-10-02  |  8.8 KB  |  306 lines

  1. /*
  2. **        $PROJECT: ConfigFile.library
  3. **        $FILE: Open&Close.c
  4. **        $DESCRIPTION: cf_Open() and cf_Close() functions
  5. **
  6. **        (C) Copyright 1996-1997 Marcel Karas
  7. **             All Rights Reserved.
  8. */
  9.  
  10. IMPORT struct ExecBase        * SysBase;
  11. IMPORT struct DosLibrary    * DOSBase;
  12.  
  13. /****** configfile.library/cf_Open *******************************************
  14. *
  15. *   NAME
  16. *        cf_Open -- Open a CF file.
  17. *
  18. *   SYNOPSIS
  19. *        Header = cf_Open(Name,Mode,ErrorCode);
  20. *        D0               A0   D0   A1
  21. *
  22. *        CFHeader * cf_Open(STRPTR,ULONG,ULONG *);
  23. *
  24. *   FUNCTION
  25. *        This function create a memory pool with the default size of 2048
  26. *        bytes, allocate pool memory for the header, open or create a new
  27. *        CF file and check which format type has the file (ascii or short
  28. *        format). And if the flag CF_OFLG_READ_TOO set, the file will be
  29. *        read too.
  30. *
  31. *   INPUTS
  32. *        Name - Name and path of the CF file.
  33. *        Mode - Open modes for the file:
  34. *    
  35. *                CF_OMODE_OLDFILE   - An existing file is opened. Did the
  36. *                                     file not exists the function failed. 
  37. *                CF_OMODE_NEWFILE   - A new file will be create.
  38. *                CF_OMODE_READWRITE - Opens a file, but creates it if it
  39. *                                     didn't exist.
  40. *
  41. *                Extra open flags: (V2)
  42. *
  43. *                CF_OFLG_READ_TOO   - Reads the file directly after the
  44. *                                     it is open. You didn't need use
  45. *                                     cf_Read().
  46. *
  47. *        ErrorCode - Contains an errorcode if the function return FALSE 
  48. *                    or NULL.
  49. *
  50. *                CF_OERR_UNKOWN     - Unkown failure.
  51. *                CF_OERR_OPEN_FILE  - Couldn't open CF file.
  52. *                CF_OERR_READ_FILE  - Couldn't read CF file.
  53. *                CF_OERR_NO_FORMAT  - File is no in CF format.
  54. *                CF_OERR_NO_SIZE    - File has no size.
  55. *                CF_OERR_HEADER_MEM - No memory for Header.
  56. *
  57. *                If the CF_OFLG_READ_TOO flag set:
  58. *
  59. *                CF_RERR_FORMAT       - File has an error in the format
  60. *                                       structure. (V2)
  61. *                CF_RERR_UNKOWN_ITYPE - An unkown item type was found. (V2)
  62. *
  63. *   RESULT
  64. *        Header - a pointer to an initialized CFHeader structure, or NULL if
  65. *                 the CF file could not be opened. In the case of a NULL
  66. *                 return, the ErrorCode var can be read to obtain more
  67. *                 information on the failure.
  68. *
  69. *   EXAMPLE
  70. *        ULONG Error;
  71. *        CFHeader *myHeader;
  72. *
  73. *        if(myHeader = cf_Open("DH0:misc/text1.cfg",CF_OMODE_NEWFILE,&Error))
  74. *        {
  75. *           ...
  76. *           cf_Close(Header);
  77. *        }
  78. *        else
  79. *        {
  80. *           switch(Error)
  81. *           {
  82. *              case CF_OERR_OPEN_FILE:  CleanUp ("Couldn't open CF file.");
  83. *              case CF_OERR_READ_FILE:  CleanUp ("Couldn't read CF file.");
  84. *              case CF_OERR_NO_FORMAT:  CleanUp ("File is no in CF format.");
  85. *              case CF_OERR_NO_SIZE:    CleanUp ("File has no size.");
  86. *              case CF_OERR_HEADER_MEM: CleanUp ("No memory for Header.");
  87. *              default:                 CleanUp ("Unkown failure.");
  88. *           }
  89. *        }
  90. *        
  91. *        ...
  92. *        
  93. *   NOTES
  94. *        If you want to open a CF file with a specify puddlesize, use the
  95. *        cf_OpenPS() function.
  96. *
  97. *   SEE ALSO
  98. *        cf_Close(), cf_Read(), cf_Write(), <libraries/configfile.h>,
  99. *        cf_OpenPS(), exec.library/CreatePool()
  100. *
  101. ******************************************************************************
  102. *
  103. */
  104.  
  105. SLibCall iCFHeader * cf_Open ( REGA0 STRPTR Name , REGD0 ULONG Mode ,
  106.     REGA1 ULONG *ErrorCode )
  107. { return (cf_OpenPS (Name, Mode, ErrorCode, 0)); }
  108.  
  109. /****** configfile.library/cf_OpenPS *****************************************
  110. *
  111. *   NAME
  112. *        cf_OpenPS -- Open a CF file with the specified puddlesize. (V2)
  113. *
  114. *   SYNOPSIS
  115. *        Header = cf_OpenPS(Name,Mode,ErrorCode,PuddleSize);
  116. *        D0                 A0   D0   A1        D1
  117. *
  118. *        CFHeader * cf_Open(STRPTR,ULONG,ULONG *,ULONG);
  119. *
  120. *   FUNCTION
  121. *        This function create a memory pool with a specify puddlesize,
  122. *        allocate pool memory for the header, open or create a new CF file
  123. *        and check which format type has the file (ascii or short format).
  124. *        And if the flag CF_OFLG_READ_TOO set, the file will be read too.
  125. *
  126. *   INPUTS
  127. *        Name - Name and path of the CF file.
  128. *        Mode - Openmode for the file.
  129. *        ErrorCode - Contains an errorcode if the function return FALSE
  130. *                    or NULL.
  131. *        PuddleSize - Size of the puddle or NULL for default (2048 bytes).
  132. *
  133. *   RESULT
  134. *        Header - a pointer to an initialized CFHeader, or NULL if the CF
  135. *                 file could not be opened. In the case of a NULL return,
  136. *                 the ErrorCode var can be read to obtain more information 
  137. *                 on the failure.
  138. *
  139. *   EXAMPLE
  140. *        CFHeader *myHeader;
  141. *
  142. *        if(myHeader = cf_OpenPS("HD3:sys.cfg",CF_OMODE_NEWFILE,0,4096))
  143. *        {
  144. *           ...
  145. *           cf_Close(Header);
  146. *        }
  147. *        
  148. *        ...
  149. *        
  150. *   SEE ALSO
  151. *        cf_Close(), cf_Read(), cf_Write(), <libraries/configfile.h>,
  152. *        cf_Open(), exec.library/CreatePool()
  153. *
  154. ******************************************************************************
  155. *
  156. */
  157.  
  158. LibCall iCFHeader * cf_OpenPS ( REGA0 STRPTR Name , REGD0 ULONG Mode ,
  159.     REGA1 ULONG * ErrorCode , REGD1 ULONG PuddleSize )
  160. {
  161.     APTR    MemPool;
  162.     ULONG    Error, MemSize = AvailMem (MEMF_LARGEST);
  163.  
  164.     FuncDe(bug("cf_Open(\"%ls\",%ld,$%08lx,%ld)\n{\n", Name, Mode, ErrorCode, PuddleSize));
  165.  
  166.     PuddleSize = !PuddleSize || ( PuddleSize < 256 ) ? MemSize / 100 : PuddleSize;
  167.  
  168.     if ( MemPool = MyCreatePool (MEMF_ANY, PuddleSize, PuddleSize) )
  169.     {
  170.         iCFHeader * Header;
  171.         BPTR    FH;
  172.         UBYTE    Cnt, ReadToo = FALSE;
  173.  
  174.         Header = MyAllocPooled (MemPool, sizeof (struct iCFHeader));
  175.  
  176.         if ( Mode & CF_OFLG_READ_TOO )
  177.             ReadToo = TRUE;
  178.  
  179.         Mode &= ~0xFFFFFFFCL;
  180.  
  181.         if ( FH = Open (Name, OModes[Mode]) )
  182.         {
  183.             NewList ((struct List *) &Header->GroupList);
  184.  
  185.             Header->OpenMode        = Mode;
  186.             Header->WBufLength    = MemSize / 10;
  187.             Header->Flags            = 0;
  188.             Header->FileHandle    = FH;
  189.             Header->PuddleSize    = PuddleSize;
  190.             Header->MemPool        = MemPool;
  191.             Header->ArryNum        = 0;
  192.             Header->ExtFlags        = 0;
  193.  
  194.             if ( Mode == CF_OMODE_NEWFILE )
  195.             {
  196.                 Header->Length = 0;
  197.                 Header->Flags |= CF_HFLG_ASCII_FILE;
  198.  
  199.                 goto OnReturn;
  200.             }
  201.             else
  202.             {
  203.                 if ( ( Header->Length = GetFileSize (FH) ) || ( Mode == CF_OMODE_READWRITE ) )
  204.                 {
  205.                     if ( ( Header->Length < 4 ) && ( Mode == CF_OMODE_READWRITE ) )
  206.                     {
  207.                         Header->Flags |= CF_HFLG_ASCII_FILE;
  208.  
  209.                         goto OnReturn;
  210.                     }
  211.                     else
  212.                     {
  213.                         __aligned UBYTE Array[CF_IDENT_EXTLEN];
  214.                         UBYTE        Format;
  215.                         ULONG    *    ID;
  216.  
  217.                         if ( !( Read (FH, Array, ( sizeof(ULONG) + sizeof(UBYTE) )) == -1 ) )
  218.                         {
  219.                             ID = (ULONG *) Array;
  220.  
  221.                             if ( *ID == CF_IDENT ) // 'CFFT' -> ConfigFileFormaT
  222.                             {
  223.                                 Format = Array[4];
  224.  
  225.                                 if ( Format == 0x0A )
  226.                                     Header->Flags |= CF_HFLG_ASCII_FILE;
  227.                                 else if ( Format == 0x00 )
  228.                                     Header->Flags |= CF_HFLG_SHORT_FILE;
  229.                                 else
  230.                                 {
  231.                                     Error = CF_OERR_UNKOWN;
  232.                                     goto OnError;
  233.                                 }
  234.  
  235.                                 if ( ReadToo && !cf_Read (Header, &Error) )
  236.                                 {
  237.                                     if ( ( Error != CF_RERR_FORMAT ) && 
  238.                                             ( Error != CF_RERR_UNKOWN_ITYPE ) )
  239.                                         Error = CF_OERR_READ_FILE;
  240.  
  241.                                     goto OnError;
  242.                                 }
  243. OnReturn:
  244.                                 FuncDe(bug("   return($%08lx)\n}\n", Header));
  245.                                 return (Header);
  246.                             }
  247.                             else    Error = CF_OERR_NO_FORMAT;
  248.                         }
  249.                         else    Error = CF_OERR_READ_FILE;
  250.                     }
  251.                 }
  252.                 else    Error = CF_OERR_NO_SIZE;
  253.             }
  254. OnError:            
  255.             Close (FH);
  256.         }
  257.         else    Error = CF_OERR_OPEN_FILE;
  258.  
  259.         MyDeletePool (MemPool);
  260.     }
  261.     else    Error = CF_OERR_HEADER_MEM;
  262.  
  263.     if ( ErrorCode ) *ErrorCode = Error;
  264.     FuncDe(bug("   return(%ld)\n}\n", Error));
  265.     return (NULL);
  266. }
  267.  
  268. /****** configfile.library/cf_Close ******************************************
  269. *
  270. *   NAME
  271. *        cf_Close -- Close a CF file.
  272. *
  273. *   SYNOPSIS
  274. *        cf_Close(Header);
  275. *                 A0
  276. *
  277. *        VOID cf_Close(CFHeader *);
  278. *
  279. *   FUNCTION
  280. *        This function close the CF file, deletes the private memory pool
  281. *        and if the CF_HFLG_WRITE_BY_CLOSE and CF_HFLG_CHANGED flags set,
  282. *        the CF file will be write too.
  283. *
  284. *   INPUTS
  285. *        Header - The Header of the file to close.
  286. *
  287. *   SEE ALSO
  288. *        cf_Open(), cf_Read(), cf_Write()
  289. *
  290. ******************************************************************************
  291. *
  292. */
  293.  
  294. LibCall VOID cf_Close( REGA0 iCFHeader * Header )
  295. {
  296.     FuncDe(bug("cf_Close($%08lx)\n{\n", Header));
  297.  
  298.     if ( Header->Flags & CF_HFLG_WRITE_BY_CLOSE )
  299.         cf_Write(Header,CF_WMODE_DEFAULT,0);
  300.  
  301.     Close (Header->FileHandle);
  302.     MyDeletePool (Header->MemPool);
  303.  
  304.     FuncDe(bug("}\n"));
  305. }
  306.